import Coordinates from "../Coordinates";
import IconFramesMgr from "../IconFramesMgr";
import { ITask } from "../logic/ITask";
import LevelLogic from "../logic/LevelLogic";
import { TaskLogin } from "../logic/TaskLogin";
import { TaskPassLevels } from "../logic/TaskPassLevels";
import { TaskSpin } from "../logic/TaskSpin";
import { TaskUseHints } from "../logic/TaskUseHints";
import NativeCaller from "../NativeCaller";
import PlayerDataMgr from "../PlayerDataMgr";
import ResManager from "../ResManager";
import Util from "../../../StartAb/Util";
import WXAD from "../../../StartAb/WXAd";

/**
 * 初始化任务列表
 * 检查完成
 * 到期重置
 * 完成领奖
 */

export default class DailyTaskSystem {
    static taskList: ITask[] = [];

    // static todayPassLevelCount = 0;
    // static keyTodayPassLevelCnt = "keyTodayPassLevelCnt"
    // static todayUseHintCount = 0;
    // static keyTodayUseHintCnt = "keyTodayUseHintCnt"

    // static lastLoginRewardTimeStamp = 0;
    // static keyLastLoginRewardTimeStamp = "keyLastLoginRewardTimeStamp"

    static keyDailyInfo = "keyDailyInfo"
    static dailyInfo = {
        todayPassLevelCount: 0,
        todayUseHintCount: 0,
        rewardGotTask: [0, 0, 0, 0, 0]
    };

    static load() {
        // this.todayPassLevelCount = parseInt(cc.sys.localStorage.getItem(this.keyTodayPassLevelCnt) || 0);
        // this.todayUseHintCount = parseInt(cc.sys.localStorage.getItem(this.keyTodayUseHintCnt) || 0);
        // this.lastLoginRewardTimeStamp = parseInt(cc.sys.localStorage.getItem(this.keyLastLoginRewardTimeStamp) || 0);
        let dailyInfoStr = cc.sys.localStorage.getItem(this.keyDailyInfo);
        if(dailyInfoStr) {
            this.dailyInfo = JSON.parse(dailyInfoStr); 
        }
    }

    static persist() {
        // cc.sys.localStorage.setItem(this.keyTodayPassLevelCnt, this.todayPassLevelCount);
        // cc.sys.localStorage.setItem(this.keyTodayUseHintCnt, this.todayUseHintCount);
        // cc.sys.localStorage.setItem(this.keyLastLoginRewardTimeStamp, this.lastLoginRewardTimeStamp);
        cc.sys.localStorage.setItem(this.keyDailyInfo, JSON.stringify(this.dailyInfo));
    }

    static init() {
        this.load();
        this.initTaskList();
    }

    static initTaskList() {
        this.taskList = [];
        this.taskList.push(new TaskLogin());
        this.taskList.push(new TaskPassLevels(5, 50));
        this.taskList.push(new TaskPassLevels(10, 100));
        this.taskList.push(new TaskPassLevels(20, 200));
        this.taskList.push(new TaskUseHints(5, 100));
        this.taskList.push(new TaskUseHints(10, 200));
        this.taskList.push(new TaskSpin(1, 50));
    }

    //
    static updateLoginRewardTime() {
        // this.lastLoginRewardTimeStamp = Date.now();
        // cc.sys.localStorage.setItem(this.keyLastLoginRewardTimeStamp, this.lastLoginRewardTimeStamp);
    }

    static passLevel() {
        // this.todayPassLevelCount++;
        this.dailyInfo.todayPassLevelCount++;
        // cc.sys.localStorage.setItem(this.keyTodayPassLevelCnt, this.todayPassLevelCount);
    }
    static useHint() {
        this.dailyInfo.todayUseHintCount++;
        // cc.sys.localStorage.setItem(this.keyTodayUseHintCnt, this.todayUseHintCount);
    }

    static resetTaskListForNewDay() {
        this.dailyInfo = {
            todayPassLevelCount: 0,
            todayUseHintCount: 0,
            rewardGotTask: [0, 0, 0, 0, 0]
        };
        this.initTaskList();
        
        console.log("******DailyTaskSystem itsANewDay**********")
    }

    static isTaskGetRewarded(taskIndex:number) {
        return this.dailyInfo.rewardGotTask[taskIndex];
    }

    static requestGetReward(taskIndex:number) {
        this.dailyInfo.rewardGotTask[taskIndex] = 1;
    }

    static goToPlayGame() {
        let currentPlayLevel = LevelLogic.getCurrentPlayLevel();
        let isHasNextLevel = (currentPlayLevel < ResManager.getLevelResArray().length);
        if (isHasNextLevel) {
            Coordinates.loadLvCoordinate(currentPlayLevel, () => {
                ResManager.currentLevelIndex = currentPlayLevel
                PlayerDataMgr.getInstance().setLastLevelIndex(currentPlayLevel)
                Util.changeScene("game");
                WXAD.instance.hideBanner();
            })
        } else {
            IconFramesMgr.getInstance().loadIcons(() => {
                Util.changeScene("levelList");
            })
        }
    }
}
